﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LinqToBing.Bing.Wrapper.Interfaces;
using LinqToBing.Bing.Wrapper.Enums;
using LinqToBing.Bing.Wrapper.ExtensionMethods;


namespace LinqToBing.Bing.Wrapper.Search
{
    /// <summary>
    /// Represent base search request to submit to Bing search engine.
    /// </summary>
    public abstract class SearchRequestBase : ISearchRequest
    {
        private string appId = null;
        /// <summary>
        /// Bing API Application Id 
        /// </summary>
        public string AppId
        {
            get { return appId; }
            set { appId = value; }
        }

        private string query = null;
        /// <summary>
        /// Query string to search
        /// </summary>
        public string Query
        {
            get { return query; }
            set { query = value; }
        }

        private List<Enums.SearchSource> sources = null;
        /// <summary>
        /// Source Types
        /// </summary>
        public List<Enums.SearchSource> Sources
        {
            get
            {
                return sources;
            }
            set
            {
                sources = value;
            }
        }

        private List<SearchTypeContainer> searches = null;
        public List<SearchTypeContainer> Searches
        {
            get
            {
                return searches;
            }
            set
            {
                if (searches != value)
                {
                    searches = value;
                    Sources = (searches.Count > 0) ? searches.GetSources() : null;
                }
            }
        }

        private int? count = null;
        public int? Count
        {
            get
            {
                return count;
            }
            set
            {
                count = value;
            }
        }

        private int? offset = null;
        public int? Offset
        {
            get
            {
                return offset;
            }
            set
            {
                offset = value;
            }
        }

        private bool executed = false;
        public bool Executed
        {
            get { return executed; }
            private set { executed = value; }
        }

        public Guid Guid
        {
            get { throw new NotImplementedException(); }
        }

        public string InvokeUrl
        {
            get { throw new NotImplementedException(); }
        }

        abstract public ISearchResult Execute();


        public bool CanExecute
        {
            get
            {
                bool canExecute = false;
                canExecute = !string.IsNullOrEmpty(Query)
                             &&
                             !string.IsNullOrWhiteSpace(Query)
                             &&
                             (Sources != null)
                             ;
                return canExecute;
            }
        }

        public SearchProtocol SearchProtocol
        {
            get;
            set;
        }
    }
}
